<template>
  <div class="system_integration">
    <c-title :hide="false" :text="diy_name" v-if="!openScan" />
    <div class="scanBox">
      <i class="iconfont icon-saoma"></i>
      <div class="btn" @click="scanTap">扫一扫</div>
      <!-- <div class="btn" @click="scanTap_stop">关闭</div> -->
    </div>
    <BarScan v-model="openScan" v-if="openScan" :show="openScan" ref="qrcode" @ok="getResult" @err="geterror"></BarScan>
  </div>
</template>

<script>
import BarScan from "./components/BarScan.vue";
export default {
  data() {
    return {
      result: "",
      openScan: false,
      diy_name:""
    };
  },
  components: {
    BarScan
  },
  activated() {
    this.getData();
  },
  methods: {
    getKey(url, name) {
      let _i = decodeURIComponent((new RegExp("[?|&]" + name + "=" + "([^&;]+?)(&|#|;|$)").exec(url) || [, ""])[1].replace(/\+/g, "%20")) || null;
      if (name == "i") {
        return String(_i).replace(/[^0-9]/gi, ""); //过滤i非数字
      } else {
        return _i;
      }
    },
    scanTap() {
      // this.result = "";
      this.openScan = true;
    },
    scanTap_stop() {
      this.openScan = false;
    },
    getResult(result) {
      this.openScan = false;
      let sn = this.getKey(result, "sn");
      if (!sn) return this.$toast("设备号错误");
      // 去填写用户信息
      this.$router.push(this.fun.getUrl("system_integration_formPage", { sn: sn }));
    },
    geterror(e) {
      if (e) {
        this.$toast(e);
      }
      this.openScan = false;
      // this.$refs.qrcode.stop();
    },
    async getData() {
      let { data, msg, result } = await $http.post("plugin.body-health-check.api.relate-member.index", {}, "");
      if (result) {
        this.diy_name = data.diy_name;
        this.fun.setWXTitle(data.diy_name);
      } else {
        this.$toast(msg);
      }
    }
  }
};
</script>

<style lang="scss" scoped>
#interactive.viewport {
  width: 640px;
  height: 480px;
  canvas,
  video {
    float: left;
    width: 640px;
    height: 480px;
  }
  canvas.drawingBuffer,
  video.drawingBuffer {
    margin-left: -640px;
  }
}

.canvanCodeBox {
  video {
    width: 100% !important;
    height: 100% !important;
  }
}

.system_integration {
  height: 100vh;
  padding: 0.63rem 0.75rem;
  box-sizing: border-box;
  overflow: hidden;
  min-height: 100vh;
  .scanBox {
    padding: 1.5rem 0;
    box-sizing: border-box;
    background: #ffffff;
    border-radius: 0.5rem;
    .icon-saoma {
      font-size: 8rem;
      color: #d6d6dc;
    }
    .btn {
      margin: 0 auto;
      margin-top: 2.5rem;
      width: 16.56rem;
      height: 2.5rem;
      line-height: 2.5rem;
      background: #f15353;
      border-radius: 1.28rem;
      font-weight: 500;
      font-size: 0.94rem;
      color: #ffffff;
      text-align: center;
      justify-content: center;
    }
  }
}
</style>
